﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using Autofac;
using Autofac.Integration.Mvc;
using Database;
using Database.infrastructure;
using Database.Model;

namespace MyBlogs.App_Start
{
    public class DependencyInjectionConfig
    {
        public static AutofacDependencyResolver Register()
        {
            var builder = new ContainerBuilder();

            //数据库上下文
            builder.RegisterType<BlogsEntities>();

            //注册工厂类
            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerLifetimeScope();

            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();

            //泛型仓储基类注册，以此避免直接暴露上下文
            builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepositoryBase<>));

            //注册当前项目下所有控制器
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            return new AutofacDependencyResolver(builder.Build());
        }
    }
}